<?php
namespace app\admin\controller\common;

use app\controller\Response;
use app\admin\logic\common\Auth as AuthLogic;
use app\BaseController;

class Auth extends BaseController{

    /**
     * @OA\Get(
     *   path="/admin/Common/addRoleForUser",
     *   tags={"公共/权限相关接口"},
     *   summary="为用户添加角色（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="role", in="query", description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function addRoleForUser($username,$role,$domain){
        try{
            $res = AuthLogic::addRoleForUserLogic($username,$role,$domain);
            if($res){
                $res = Response::succJson('',0,'添加角色成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'添加角色失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }

    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deleteRoleForUser",
     *   summary="删除指定用户的指定角色（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="role", in="query", description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deleteRoleForUser($username,$role,$domain){
        try{
            $res = AuthLogic::deleteRoleForUserLogic($username,$role,$domain);
            if($res){
                $res = Response::succJson('',0,'删除角色成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除角色失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }

    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deleteRolesForUser",
     *   summary="删除用户的所有角色（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deleteRolesForUser($username,$domain){
        try{
            $res = AuthLogic::deleteRolesForUserLogic($username,$domain);
            if($res){
                $res = Response::succJson('',0,'删除所有角色成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除所有角色失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/getRolesForUser",
     *   summary="获取指定用户具有的角色（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function getRolesForUser($username,$domain){
        try{
            $res = AuthLogic::getRolesForUserLogic($username,$domain);
            if($res){
                $res = Response::succJson($res,'0','获取用户关联角色成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'获取用户关联角色失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/getUsersForRole",
     *   summary="获取具有指定角色的用户（基于租户）",
     *   @OA\Parameter(name="role", in="query",  description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function getUsersForRole($role,$domain){
        try{
            $result = AuthLogic::getUsersForRoleLogic($role,$domain);
            if($result){
                $res = Response::succJson($result,'0','操作成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'操作失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/hasRoleForUser",
     *   summary="确定指定用户是否具有指定角色（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="role", in="query",  description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query", description="租户", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function hasRoleForUser($username,$role,$domain){
        try{
            $result = AuthLogic::hasRoleForUserLogic($username,$role,$domain);
            if($result){
                $res = Response::succJson($result,'0','操作成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'操作失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::errJson('',$e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/addPermissionForUser",
     *   summary="为用户或角色添加权限（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="role", in="query",  description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query",  description="租户", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="class", in="query",  description="功能类", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="permission", in="query", description="功能类权限", @OA\Schema(type="int")),
     *   @OA\Parameter(name="menu_id", in="query", description="菜单id", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function addPermissionForUser($username,$role,$domain,$class,$permission,$menu_id){
        try{
            $res = AuthLogic::addPermissionForUserLogic($username,$role,$domain,$class,$permission,$menu_id);
            if($res){
                $res = Response::succJson('',0,'添加成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'添加失败或已添加！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deletePermissionForUser",
     *   summary="删除用户或角色的指定权限（基于租户）",
     *   @OA\Parameter(name="username_or_role", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query",  description="租户", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="class", in="query",  description="功能类", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="permission", in="query", description="功能类权限", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deletePermissionForUser($username_or_role,$domain,$class,$permission){
        try{
            $result = AuthLogic::deletePermissionForUserLogic($username_or_role,$domain,$class,$permission);
            if($result){
                $res = Response::succJson('','0','删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deletePermissionsForUser",
     *   summary="删除用户或角色的所有权限",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deletePermissionsForUser($username){
        try{
            $result = AuthLogic::deletePermissionsForUserLogic($username);
            if($result){
                $res = Response::succJson('','0','删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('','200','删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/getPermissionsForUser",
     *   summary="获取用户或角色的所有权限（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="role", in="query",  description="角色", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query",  description="租户", @OA\Schema(type="varchar")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function getPermissionsForUser($username="",$role="",$domain=""){
        try{
            $res = AuthLogic::getPermissionsForUserLogic($username,$role,$domain);
            if($res){
                $res = Response::succJson($res,0,'获取成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'获取失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/hasPermissionForUser",
     *   summary="确定用户是否具有指定权限（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="domain", in="query",  description="租户", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="calss", in="query",  description="功能类", @OA\Schema(type="varchar")),
     *   @OA\Parameter(name="permission", in="query", description="功能类权限", @OA\Schema(type="int")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function hasPermissionForUser($username,$domain,$class,$permission){
        try{
            $result = AuthLogic::hasPermissionForUserLogic($username,$domain,$class,$permission);
            if($result){
                $res = Response::succJson('','0','操作成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'操作失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }

    /**
 * @OA\Get(
 *   tags={"公共/权限相关接口"},
 *   path="/admin/Common/deleteRole",
 *   summary="删除角色（基于租户）",
 *   @OA\Parameter(name="role", in="query",  description="角色", @OA\Schema(type="varchar")),
 *   @OA\Response(
 *     response="default",
 *     description="an ""unexpected"" error"
 *   )
 * )
 */
    public function deleteRole($role){
        try{
            $result = AuthLogic::deleteRoleLogic($role);
            if($result){
                $res = Response::succJson('','0','删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deleteUser",
     *   summary="删除用户（基于租户）",
     *   @OA\Parameter(name="username", in="query",  description="用户名", @OA\Schema(type="varchar")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deleteUser($username){
        try{
            $result = AuthLogic::deleteUserLogic($username);
            if($result){
                $res = Response::succJson('','0','删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }
    /**
     * @OA\Get(
     *   tags={"公共/权限相关接口"},
     *   path="/admin/Common/deletePermission",
     *   summary="删除权限（基于租户）",
     *   @OA\Parameter(name="premission", in="query",  description="权限", @OA\Schema(type="varchar")),
     *   @OA\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    public function deletePermission($premission){
        try{
            $result = AuthLogic::deletePermissionLogic($premission);
            if($result){
                $res = Response::succJson('','0','删除成功！');
                return json($res);
            }else{
                $res = Response::errJson('',200,'删除失败！');
                return json($res);
            }
        }catch (\Exception $e){
            $res = Response::exceptionJson($e->getCode(),$e->getMessage());
            return json($res);
        }
    }


}